import type { Deployment } from "../../../api/k8s/types";

import { useK8SApi } from "../../../api/k8s/k8s";
import { SourceMan } from "../providers/sourceCtl";
import { onMounted, type Ref } from "vue";
import type { DeploymentQuery } from "@/views/domain/kubeHud/api/k8s/deployment";
import { useRoute } from "vue-router";
const { deploymentRepo } = useK8SApi();

class DeploymentProvider extends SourceMan<Deployment> {
  get deploymentList(): Ref<Deployment[]> {
    return this.srcList;
  }

  // 获取节点列表
  getDeployments = async (req?: DeploymentQuery) => {
    const res = await deploymentRepo.getDeploymentList({
      ...(req || this.searchQuery.value),
      continue: this.continue,
      limit: this.pager.limit.value
    });
    if (res.ok) {
      this.continue = res.data.metadata.continue;
      this.srcList.value = res.data.items;
      this.pager.total.value =
        res.data.metadata.remainingItemCount ?? res.data.items.length;
    }
  };

  // 获取节点详情
  getDeploymentDetails = async (query: {
    namespace?: string;
    name?: string;
  }) => {
    const res = await deploymentRepo.getDeploymentDetail(query);
    if (res.ok) {
      this.srcDetail.value = res.data;
      this.srcYamlContent.value = this.objectToYaml(res.data);
      this.yamlDialog.value = true;
    }
  };

  // 更新节点
  updateDeployment = async (data?: { namespace: string; name: string }) => {
    const res = await deploymentRepo.updateDeployment(data);
    if (res.ok) {
      this.refreshDeploymentList();
    }
  };

  // delete
  deleteDeployment = async (query?: DeploymentQuery) => {
    const res = await deploymentRepo.deleteDeployment(query);
    if (res.ok) {
      this.refreshDeploymentList();
    }
  };

  refreshDeploymentList = async () => {
    this.continue = undefined;
    await this.getDeployments({});
  };

  handleScale = async (info?: Deployment, num?: number) => {
    console.log("handleScale", info, num);
    return;
  };

  restartDeployment = async (info?: Deployment) => {
    console.log("restartDeployment", info);
    return;
  };

  createRules = () => {
    return {
      name: [
        {
          required: true,
          message: "请填写名称",
          trigger: "change"
        }
      ],
      image: [
        {
          required: true,
          message: "请填写镜像",
          trigger: "change"
        }
      ],
      namespace: [
        {
          required: true,
          message: "请选择命名空间",
          trigger: "change"
        }
      ],
      resource: [
        {
          required: true,
          message: "请选择配额",
          trigger: "change"
        }
      ],
      label_str: [
        {
          required: true,
          message: "请填写标签",
          trigger: "change"
        }
      ],
      container_port: [
        {
          required: true,
          message: "请填写容器端口",
          trigger: "change"
        }
      ]
    };
  };
}
export const useDeployProvider = () => {
  const route = useRoute();
  const provider = new DeploymentProvider();

  // 根据路由获取详情
  const getDeployDetailsFromRoute = async () => {
    return provider.getDeploymentDetails({
      namespace: `${route.query.namespace ?? ""}`,
      name: `${route.query.name ?? ""}`
    });
  };

  onMounted(getDeployDetailsFromRoute);
  return {
    deployProvider: provider,
    loadingWrapper: provider.loadingWrapper,
    getDeployDetailsFromRoute,
    route
  };
};
